package cn.iocoder.yudao.module.xhyy.controller.admin.exam;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.xhyy.controller.admin.exam.vo.*;
import cn.iocoder.yudao.module.xhyy.dal.dataobject.exam.ExamDO;
import cn.iocoder.yudao.module.xhyy.service.exam.ExamService;

@Tag(name = "管理后台 - 试卷资源")
@RestController
@RequestMapping("/xhyy/exam")
@Validated
public class ExamController {

    @Resource
    private ExamService examService;

    @PostMapping("/create")
    @Operation(summary = "创建试卷资源")
    @PreAuthorize("@ss.hasPermission('xhyy:exam:create')")
    public CommonResult<String> createExam(@Valid @RequestBody ExamSaveReqVO createReqVO) {
        return success(examService.createExam(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新试卷资源")
    @PreAuthorize("@ss.hasPermission('xhyy:exam:update')")
    public CommonResult<Boolean> updateExam(@Valid @RequestBody ExamSaveReqVO updateReqVO) {
        examService.updateExam(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除试卷资源")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('xhyy:exam:delete')")
    public CommonResult<Boolean> deleteExam(@RequestParam("id") String id) {
        examService.deleteExam(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得试卷资源")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('xhyy:exam:query')")
    public CommonResult<ExamRespVO> getExam(@RequestParam("id") String id) {
        ExamDO exam = examService.getExam(id);
        return success(BeanUtils.toBean(exam, ExamRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得试卷资源分页")
    @PreAuthorize("@ss.hasPermission('xhyy:exam:query')")
    public CommonResult<PageResult<ExamRespVO>> getExamPage(@Valid ExamPageReqVO pageReqVO) {
        PageResult<ExamDO> pageResult = examService.getExamPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, ExamRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出试卷资源 Excel")
    @PreAuthorize("@ss.hasPermission('xhyy:exam:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportExamExcel(@Valid ExamPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ExamDO> list = examService.getExamPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "试卷资源.xls", "数据", ExamRespVO.class,
                        BeanUtils.toBean(list, ExamRespVO.class));
    }

}